package com.yanqu.road.server.manager.banquet.baifubanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquet;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetData;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetJoinInfoData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.crossbanquet.CrossGoodBanquetBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description 百福宴会
 * @Author cwq
 * @Data 2021/11/9 10:20
 */
public class CrossGoodBanquetMgr extends TempMgr {

    public static Logger logger = LogManager.getLogger(CrossGoodBanquetMgr.class.getName());

    /**
     * k:活动id
     */
    private static Map<Integer, CrossGoodBanquetActivity> crossGoodBanquetActivityMap = new ConcurrentHashMap<>();

    /**
     * 需要通知给区服的宴会结算数据
     */
    private static Map<Long,GoodBanquet> userFinishBanquetMap = new ConcurrentHashMap<>();

    /**
     * 宴会最大id
     */
    private static AtomicLong maxGoodBanquetId = new AtomicLong();

    @Override
    public boolean init() throws Exception {
        maxGoodBanquetId = new AtomicLong(CrossGoodBanquetBussiness.getMaxGoodBanquetId());
        reload();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        Map<Integer,ActivityInfo> activityInfoMap = ActivityMgr.getRunningCrossBanquetRankActivity();
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            CrossGoodBanquetActivity activity = getCrossGoodBanquetActivity(activityInfo.getActivityId());
            List<GoodBanquetData> banquetList = CrossGoodBanquetBussiness.getGoodBanquetDataList(activityInfo.getActivityId());
            for (GoodBanquetData item : banquetList){
                GoodBanquet goodBanquet = new GoodBanquet(item);
                if(!goodBanquet.getBanquetData().getIsSettlement()){
                    activity.getBanquetList().add(goodBanquet);
                }
                activity.getBanquetMap().put(item.getId(),goodBanquet);
                List<GoodBanquet> userGoodBanquetList = activity.getUserBanquetList(item.getUserId(),true);
                userGoodBanquetList.add(goodBanquet);
            }
            List<GoodBanquetJoinInfoData> joinInfoList = CrossGoodBanquetBussiness.getGoodBanquetJoinInfoList(activityInfo.getActivityId());
            for (GoodBanquetJoinInfoData item : joinInfoList){
                GoodBanquet goodBanquet = activity.getBanquetMap().get(item.getBanquetId());
                if(goodBanquet == null){
                    logger.error("存在有参赛记录，没有百福宴记录的情况。activityId:{},banquetId:{}",item::getActivityId,item::getBanquetId);
                    continue;
                }
                goodBanquet.getJoinMap().put(item.getUserId(),item);
                goodBanquet.getJoinInfoList().add(item);
                long maxId = activity.getMaxJoinBanquetIdMap().getOrDefault(item.getUserId(),0L);
                activity.getMaxJoinBanquetIdMap().put(item.getUserId(),Math.max(maxId,item.getBanquetId()));
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (CrossGoodBanquetActivity item : crossGoodBanquetActivityMap.values()){
            for (GoodBanquet goodBanquet : item.getBanquetMap().values()){
                if (goodBanquet.getBanquetData().isUpdateOption()) {
                    CrossGoodBanquetBussiness.updateGoodBanquetData(goodBanquet.getBanquetData());
                } else if (goodBanquet.getBanquetData().isInsertOption()) {
                    CrossGoodBanquetBussiness.addGoodBanquetData(goodBanquet.getBanquetData());
                }

                List<GoodBanquetJoinInfoData> addJoinList = new ArrayList<>();
                for (GoodBanquetJoinInfoData joinInfoData : goodBanquet.getJoinMap().values()){
                    if (joinInfoData.isInsertOption()) {
                        addJoinList.add(joinInfoData);
                    }
                }
                if(addJoinList.size() >0 ){
                    CrossGoodBanquetBussiness.addGoodBanquetJoinInfoDataList(addJoinList);
                }
            }
        }
        return true;
    }

    /**
     * 开启百福宴会
     */
    public static CrossBanquetProto.GoodBanquetOpenToGameServerMsg.Builder openGoodBanquet(int activityId, long userId, int type,UserBaseInfo userBaseInfo) {
        CrossBanquetProto.GoodBanquetOpenToGameServerMsg.Builder builder = CrossBanquetProto.GoodBanquetOpenToGameServerMsg.newBuilder();
        builder.setType(type);
        BanquetInfo banquetInfo = CrossBanquetConfigMgr.getBanquetInfo(type);
        if(banquetInfo == null){
            builder.setRet(GameErrorCode.E_BANQUET_INFO_ERROR);
            return builder;
        }
        CrossGoodBanquetActivity banquetActivity = getCrossGoodBanquetActivity(activityId);
        if(banquetActivity == null){
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);
        long endTime = 0;
        synchronized (banquetActivity){
            // 开启有上限
            int num = getUserOpeningNum(banquetActivity,userId);
            if(num >= GameConfig.BANQUET_GOOD_OPEN_MAX_NUM){
                builder.setRet(GameErrorCode.E_BANQUET_OPEN_LIMIT);
                return builder;
            }
            GoodBanquetData goodBanquet = banquetActivity.openGoodBanquet(activityId, userId, type);
            endTime = goodBanquet.getEndTime();
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo != null){
            // 同步给参与区服
            CrossBanquetProto.GoodBanquetOpenMsg.Builder openSynMsg = CrossBanquetProto.GoodBanquetOpenMsg.newBuilder();
            openSynMsg.setEndTime(endTime);
            builder.setEndTime(endTime);
            for (long serverId : activityInfo.getServerIdList()){
                MessageHelper.sendPacket(serverId ,0, YanQuMessageUtils.buildMessage(GameProtocol.S_GOOD_BANQUET_OPEN_SYN_FROM_CROSS, openSynMsg));
            }
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 获取正在开启的宴会
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static CrossBanquetProto.GoodBanquetListRespMsg.Builder getOpenGoodBanquetList(int activityId, long userId, int type) {
        CrossBanquetProto.GoodBanquetListRespMsg.Builder builder = CrossBanquetProto.GoodBanquetListRespMsg.newBuilder();
        CrossGoodBanquetActivity banquetActivity = getCrossGoodBanquetActivity(activityId);
        if (banquetActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        List<CrossBanquetProto.GoodBanquetListTemp> banquetListTempList = new ArrayList<>();
        int now = DateHelper.getCurrentSecond();
        if (type == 0) {
            // 所有正在开启的宴会  未参与的  已参与的
            // 已参与的宴会
            List<GoodBanquet> joinedTempList = new ArrayList<>();
            for (GoodBanquet item : banquetActivity.getBanquetList()) {
                // 最多显示n条
                if (banquetListTempList.size() >= GameConfig.BANQUET_GOOD_SHOW_OPENING_MAX_NUM) {
                    break;
                }
                if(isOverGoodBanquet(now, item)){
                    continue;
                }
                if (!item.getJoinMap().containsKey(userId)) {
                    // 未参与
                    banquetListTempList.add(builderGoodBanquetListTemp(item, true,true));
                } else {
                    // 已参与
                    joinedTempList.add(item);
                }
            }
            for (GoodBanquet item : joinedTempList) {
                // 下发已赴宴的
                if (banquetListTempList.size() >= GameConfig.BANQUET_GOOD_SHOW_OPENING_MAX_NUM) {
                    break;
                }
                banquetListTempList.add(builderGoodBanquetListTemp(item, false,true));
            }
        } else {
            // 玩家自己的宴会 需要下发历史宴会
            List<GoodBanquet> userList = banquetActivity.getUserBanquetList(userId, false);
            if (userList != null) {
                for (GoodBanquet item : userList) {
                    boolean canJoin = !item.getJoinMap().containsKey(userId);
                    banquetListTempList.add(builderGoodBanquetListTemp(item, canJoin, false));
                }
            }
        }
        if(banquetListTempList.size() > 0){
            builder.addAllBanquetList(banquetListTempList);
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 赴宴
     *
     * @param activityId
     * @param userId
     * @param banquetId
     * @return
     */
    public static CrossBanquetProto.GoodBanquetJoinRespToGameServerMsg.Builder joinGoodBanquet(int activityId, long userId, long banquetId,UserBaseInfo userBaseInfo) {
        CrossBanquetProto.GoodBanquetJoinRespToGameServerMsg.Builder builder = CrossBanquetProto.GoodBanquetJoinRespToGameServerMsg.newBuilder();
        CrossGoodBanquetActivity banquetActivity = getCrossGoodBanquetActivity(activityId);
        if(banquetActivity == null){
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        CrossUserMgr.updateUserBaseInfo(userId,userBaseInfo);
        int now = DateHelper.getCurrentSecond();
        List<GoodBanquet> goodBanquetList = new ArrayList<>();
        // 赴宴玩家的信息  k:userId k:type v:位置集合
        Map<Long,Map<Integer,List<Integer>>> resultMap = new HashMap<>();
        synchronized (banquetActivity){
            if(banquetId == 0){
                // 一键赴宴
                int size = banquetActivity.getBanquetList().size();
                if(size > 0){
                    long maxId = banquetActivity.getBanquetList().get(size - 1).getBanquetData().getId();
                    long maxJoinBanquetId = banquetActivity.getMaxJoinBanquetIdMap().getOrDefault(userId,0L);
                    for (int i = size; i > 0; i--) {
                        GoodBanquet goodBanquet = banquetActivity.getBanquetList().get(i-1);
                        if(goodBanquet.getBanquetData().getId() <= maxJoinBanquetId){
                            break;
                        }
                        int ret = canJoinBanquet(now,userId,goodBanquet);
                        if(ret == 0){
                            goodBanquetList.add(goodBanquet);
                        }
                    }
                    if(goodBanquetList.size() >0 ){
                        banquetActivity.getMaxJoinBanquetIdMap().put(userId,maxId);
                    }
                }
            }else {
                // 单个赴宴
                GoodBanquet goodBanquet = banquetActivity.getBanquetMap().get(banquetId);
                int ret = canJoinBanquet(now,userId,goodBanquet);
                if(ret != 0){
                    builder.setRet(ret);
                    return builder;
                }
                goodBanquetList.add(goodBanquet);
            }
            for (GoodBanquet item : goodBanquetList){
                // 赴宴
                GoodBanquetJoinInfoData banquetJoinInfo = new GoodBanquetJoinInfoData();
                banquetJoinInfo.setBanquetId(item.getBanquetData().getId());
                banquetJoinInfo.setActivityId(item.getBanquetData().getActivityId());
                banquetJoinInfo.setUserId(userId);
                banquetJoinInfo.setTime(now);
                banquetJoinInfo.setInsertOption();

                item.getJoinMap().put(userId,banquetJoinInfo);
                item.getJoinInfoList().add(banquetJoinInfo);
                // 第几个赴宴
                int pos = item.getJoinInfoList().size();

                Map<Integer,List<Integer>> map = resultMap.get(item.getBanquetData().getUserId());
                if(map == null){
                    map = new HashMap<>();
                    resultMap.put(item.getBanquetData().getUserId(), map);
                }
                List<Integer> list = map.get(item.getBanquetData().getType());
                if(list == null){
                    list = new ArrayList<>();
                    map.put(item.getBanquetData().getType(),list);
                }
                list.add(pos);
            }
        }
        if(resultMap.size() > 0){
            for (Map.Entry<Long,Map<Integer,List<Integer>>> entry : resultMap.entrySet()){
                UserBaseInfo joined = CrossUserMgr.getUserBaseInfo(entry.getKey());
                if(joined != null){
                    CrossBanquetProto.GoodBanquetJoinUserEntity.Builder joinUserEntity = CrossBanquetProto.GoodBanquetJoinUserEntity.newBuilder();
                    joinUserEntity.setName(joined.getNickName());
                    for (Map.Entry<Integer,List<Integer>> listEntry : entry.getValue().entrySet()){
                        CrossBanquetProto.GoodBanquetJoinListEntity.Builder joinListEntity = CrossBanquetProto.GoodBanquetJoinListEntity.newBuilder();
                        joinListEntity.setType(listEntry.getKey());
                        joinListEntity.addAllPosList(listEntry.getValue());
                        joinUserEntity.addJoinList(joinListEntity);
                    }
                    builder.addJoinUserList(joinUserEntity);
                }
            }
            builder.setRet(0);
        }else {
            builder.setRet(GameErrorCode.E_BANQUET_ACTIVITY_NO_CAN_JOIN);
        }
        return builder;
    }


    /**
     * 获取赴宴成员
     *
     * @param activityId
     * @param banquetId
     * @param pageNum
     * @return
     */
    public static CrossBanquetProto.GoodBanquetMemberListRespMsg.Builder getBanquetMemberList(int activityId, long banquetId, int pageNum) {
        CrossBanquetProto.GoodBanquetMemberListRespMsg.Builder builder = CrossBanquetProto.GoodBanquetMemberListRespMsg.newBuilder();
        builder.setPageNum(pageNum);
        CrossGoodBanquetActivity banquetActivity = getCrossGoodBanquetActivity(activityId);
        if (banquetActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        GoodBanquet goodBanquet = banquetActivity.getBanquetMap().get(banquetId);
        if(goodBanquet == null){
            builder.setRet(GameErrorCode.E_BANQUET_NOT_EXIST);
            return builder;
        }
        int index = GameConfig.BANQUET_GOOD_SHOW_MEMBER_PAGE_NUM * pageNum;
        int startIndex = Math.max(index, 0);
        int endIndex = index + GameConfig.BANQUET_GOOD_SHOW_MEMBER_PAGE_NUM;
        List<GoodBanquetJoinInfoData> list = new ArrayList<>();
        synchronized (banquetActivity){
            for (int i = startIndex; i < endIndex; i++) {
                if(goodBanquet.getJoinInfoList().size() <= i){
                    break;
                }
                GoodBanquetJoinInfoData info = goodBanquet.getJoinInfoList().get(i);
                list.add(info);
            }
        }
        for (GoodBanquetJoinInfoData item : list){
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(item.getUserId());
            if (userBaseInfo != null) {
                builder.addMember(CrossBanquetProto.GoodBanquetMemberMsg.newBuilder().setNickName(userBaseInfo.getNickName()).setServerId(userBaseInfo.getServerId()));
            }
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 获取玩家可以赴百福宴的集合中，结束时间最长的宴会结束时间  红点用
     * @param activityId
     * @param userId
     */
    public static void getCanJoinOpenTime(int activityId, long serverId, long userId){
        CrossGoodBanquetActivity banquetActivity = getCrossGoodBanquetActivity(activityId);
        if(banquetActivity == null){
            return;
        }
        int now = DateHelper.getCurrentSecond();
        long endTime = 0;
        List<CrossBanquetProto.GoodBanquetUserOpenEntity> userOpenEntityList = new ArrayList<>();
        synchronized (banquetActivity){
            long maxJoinBanquetId = banquetActivity.getMaxJoinBanquetIdMap().getOrDefault(userId,0L);
            for (int i = banquetActivity.getBanquetList().size(); i > 0 ; i--) {
                GoodBanquet goodBanquet = banquetActivity.getBanquetList().get(i-1);
                if (goodBanquet.getBanquetData().getId() <= maxJoinBanquetId) {
                    // 已经全部赴宴
                    break;
                }
                if(canJoinBanquet(now,userId,goodBanquet) == 0){
                    endTime = goodBanquet.getBanquetData().getEndTime();
                    break;
                }
            }
            // 获取自己开宴的信息
            List<GoodBanquet> list = banquetActivity.getUserBanquetList(userId,false);
            if(list != null){
                for (GoodBanquet item : list){
                    if(!isOverGoodBanquet(now,item)){
                        userOpenEntityList.add(CrossBanquetProto.GoodBanquetUserOpenEntity.newBuilder().setType(item.getBanquetData().getType()).setEndTime(item.getBanquetData().getEndTime()).build());
                    }
                }
            }
        }
        if(endTime > 0 || userOpenEntityList.size() > 0){
            CrossBanquetProto.GoodBanquetOpenMsg.Builder builder = CrossBanquetProto.GoodBanquetOpenMsg.newBuilder();
            builder.setEndTime(endTime);
            builder.addAllUserOpenList(userOpenEntityList);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GOOD_BANQUET_CAN_JOIN_TIME, builder));
        }
    }

    private static CrossGoodBanquetActivity getCrossGoodBanquetActivity(int activityId) {
        CrossGoodBanquetActivity banquetActivity = crossGoodBanquetActivityMap.get(activityId);
        if (banquetActivity == null) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                return null;
            }
            if(activityInShowTime(activityInfo) && activityInfo.getType() == eActivityType.CrossRankActivity.getValue() && activityInfo.getChildType() == 5){
                synchronized (crossGoodBanquetActivityMap) {
                    banquetActivity = crossGoodBanquetActivityMap.get(activityId);
                    if (banquetActivity == null) {
                        banquetActivity = new CrossGoodBanquetActivity(activityId);
                        crossGoodBanquetActivityMap.put(activityId, banquetActivity);
                    }
                }
            }
        }
        return banquetActivity;
    }

    public static AtomicLong getMaxGoodBanquetId() {
        return maxGoodBanquetId;
    }


    /**
     * @param item
     * @param canJoin 是否可以赴宴
     * @param showUserInfo 是否下发玩家信息
     * @return
     */
    private static CrossBanquetProto.GoodBanquetListTemp builderGoodBanquetListTemp(GoodBanquet item ,boolean canJoin, boolean showUserInfo){
        CrossBanquetProto.GoodBanquetListTemp.Builder builder = CrossBanquetProto.GoodBanquetListTemp.newBuilder();
        builder.setBanquetId(item.getBanquetData().getId());
        builder.setCount(item.getJoinInfoList().size());
        builder.setUserId(item.getBanquetData().getUserId());
        if(showUserInfo){
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(item.getBanquetData().getUserId());
            if(userBaseInfo != null){
                builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
        }
        builder.setCanJoin(canJoin);
        builder.setOpenTime(item.getBanquetData().getCreateTime());
        builder.setType(item.getBanquetData().getType());
        return builder.build();
    }


    /**
     * 结算百福宴奖励
     */
    public static synchronized void settlement() {
        int now = DateHelper.getCurrentSecond();
        for (CrossGoodBanquetActivity activity : crossGoodBanquetActivityMap.values()) {
            synchronized (activity){
                Iterator<GoodBanquet> iterator = activity.getBanquetList().iterator();
                while (iterator.hasNext()) {
                    GoodBanquet item = iterator.next();
                    if (!isOverGoodBanquet(now, item)) {
                        break;
                    }
                    // 移除正在开启宴会
                    activity.getBanquetList().remove(item);
                    item.getBanquetData().setNum(item.getJoinInfoList().size());
                    userFinishBanquetMap.put(item.getBanquetData().getId(),item);
                }
            }
        }

        Map<Long, CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg.Builder> settlementMap = new HashMap<>();
        for (GoodBanquet item : userFinishBanquetMap.values()) {
            // 通知区服可以领奖
            long serverId = CrossUserMgr.getServerIdByUserId(item.getBanquetData().getUserId());
            CrossBanquetProto.GoodBanquetSettlementMsg.Builder builder = CrossBanquetProto.GoodBanquetSettlementMsg.newBuilder();
            builder.setActivityId(item.getBanquetData().getActivityId());
            builder.setBanquetId(item.getBanquetData().getId());
            builder.setUserId(item.getBanquetData().getUserId());
            builder.setNum(item.getBanquetData().getNum());
            builder.setType(item.getBanquetData().getType());
            builder.setEndTime(item.getBanquetData().getEndTime());

            CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg.Builder listMsg = settlementMap.get(serverId);
            if (listMsg == null) {
                listMsg = CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg.newBuilder();
                settlementMap.put(serverId, listMsg);
            }
            listMsg.addSettlementList(builder);
        }
        for (Map.Entry<Long, CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg.Builder> entry : settlementMap.entrySet()) {
            MessageHelper.sendPacket(entry.getKey(), 0, YanQuMessageUtils.buildMessage(GameProtocol.S_GOOD_BANQUET_SETTLEMENT_FROM_CROSS, entry.getValue()));
        }
    }

    /**
     * 同步正在开启的百福宴信息
     */
    public static synchronized void synGoodBanquet(){
        int now = DateHelper.getCurrentSecond();
        for (CrossGoodBanquetActivity activity : crossGoodBanquetActivityMap.values()){
            // 判断是否是否正在开启
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activity.getActivityId());
            if(activityInfo == null){
                continue;
            }
            // 参与区服
            if(now >= activityInfo.getBeginTime() && now < activityInfo.getEndShowTime()) {
                CrossBanquetProto.GoodBanquetSyncMsg.Builder builder = CrossBanquetProto.GoodBanquetSyncMsg.newBuilder();
                for (GoodBanquet item : activity.getBanquetList()){
                    if(isOverGoodBanquet(now, item)){
                         // 过期
                        continue;
                    }
                    CrossBanquetProto.GoodBanquetCountTemp.Builder goodBanquetCountTemp = CrossBanquetProto.GoodBanquetCountTemp.newBuilder();
                    goodBanquetCountTemp.setBanquetId(item.getBanquetData().getId());
                    goodBanquetCountTemp.setCount(item.getJoinInfoList().size());
                    builder.addCountList(goodBanquetCountTemp);
                }
                if(builder.getCountListCount() > 0){
                    for (Long serverId : activityInfo.getServerIdList()){
                        MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(GameProtocol.S_GOOD_BANQUET_SYN_FROM_CROSS, builder));
                    }
                }
            }
        }
    }

    private static int canJoinBanquet(int now, long userId, GoodBanquet goodBanquet){
        if(isOverGoodBanquet(now,goodBanquet)){
            return GameErrorCode.E_BANQUET_IS_OVER;
        }
        if(goodBanquet.getJoinMap().containsKey(userId)){
            return GameErrorCode.E_BANQUET_HAS_JOIN;
        }
        return 0;
    }

    private static boolean activityInShowTime(ActivityInfo activityInfo) {
        if (null == activityInfo) {
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    private static boolean isOverGoodBanquet(int now, GoodBanquet goodBanquet){
        return goodBanquet == null || now >= goodBanquet.getBanquetData().getEndTime();
    }

    public static Map<Long, GoodBanquet> getUserFinishBanquetMap() {
        return userFinishBanquetMap;
    }

    /**
     * 获取玩家正在开启的宴会数量
     */
    public static int getUserOpeningNum(CrossGoodBanquetActivity banquetActivity,long userId){
        List<GoodBanquet> list = banquetActivity.getUserBanquetList(userId,false);
        int num = 0;
        if(list != null){
            int now = DateHelper.getCurrentSecond();
            for (GoodBanquet item : list){
                if(!isOverGoodBanquet(now,item)){
                    num++;
                }
            }
        }
        return num;
    }


}
